/**
 * Created by kevin on 2018/8/21
 * 公共mixin事件、方法、数据
 */
import wepy from 'wepy'
export default class CommonMixin extends wepy.mixin {
  data = {
    // 用户授权状态
    hasAuth: !!wx.getStorageSync('AUTH_TAG')
  }

  methods = {
    // stop event
    stopEvent() {}
  }

  onLoad() {
    // 设置用户授权状态
    this.setAuthStatus()
  }

  async onShow() {
    await this.sleep(2000)
    // 页面浏览pv上报
    wx.$track.pageViewTrackBuriedPointByTrackId({
      trackId: this.$wxpage.route,
      instance: this
    })
  }

  // 滚到页面底部事件
  onReachBottom() {
    this.$broadcast('on-change-logo-url')
  }

  // 设置用户授权状态
  setAuthStatus() {
    this.hasAuth = !!wx.getStorageSync('AUTH_TAG')
    this.$apply()
    console.log('setAuthStatus:', this.hasAuth)
  }

  // 休眠函数
  sleep(s) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve()
      }, s)
    })
  }

  // 点击事件上报
  clickEventTrack({ trackId = '' } = {}) {
    wx.$track.clickTrackBuriedPointByTrackId({
      trackId,
      instance: this
    })
  }

  // 分享事件上报
  shareEventTrack({
    trackId = '',
    shard_link = this.$wxpage.route,
    shard_openid = wx.$getGlobalData().openId,
    shard_type = 'sendAppMessage'
  } = {}) {
    wx.$track.shareTrackBuriedPointByTrackId({
      trackId,
      instance: this,
      biz: {
        shard_link,
        shard_type,
        shard_openid
      }
    })
  }
}
